<%@ WebHandler Language="C#" Class="RssGenerator" %>

using System;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using BookBarter;

public class RssGenerator : IHttpHandler {

    private string _requestUserid;
    
    /// <summary>
    /// method that activates when RssGenerator is opened
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest (HttpContext context) { //takes requests to process and outputs response        
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;
        
        _requestUserid = request.QueryString["userid"];
        if (String.IsNullOrEmpty(_requestUserid))
        { _requestUserid = "unregistered users"; } //default user
        
        //instantiate and populate notification list
        List<Notification> notifications = new List<Notification>();
        //query database
        //foreach result create a new Notification and add into notifications

        response.Write(ChannelGenerator(notifications));
    }

    /// <summary>
    /// generates the rss code to be displayed
    /// </summary>
    /// <returns></returns>
    private string ChannelGenerator(List<Notification> notifications) //generate the overall rss code
    {
        StringBuilder channel = new StringBuilder();
        channel.Append("<?xml version='1.0' encoding='utf-8'?>");
        channel.Append("<rss version='2.0'>");
        channel.Append("<channel>");
        
        channel.Append(TitleGenerator());
        foreach (Notification note in notifications)
        {
            channel.Append(ItemGenerator(note.GetTitle, note.GetMessage, note.GetLink));
        }
        
        channel.Append("</channel>");
        channel.Append("</rss>");
        
        return channel.ToString();
    }

    /// <summary>
    /// generates the rss code for the title
    /// </summary>
    /// <returns></returns>
    private string TitleGenerator() //generates the title of the feed
    {
        StringBuilder title = new StringBuilder();
        title.Append("<title>BookTrader</title>");
        title.Append("<user>").Append(_requestUserid).Append("</user>");
        title.Append("<ttl>10</ttl>");
        title.Append("<link>http://www.booktrader.com</link>");
        title.Append("<description>lastest message for ").Append(_requestUserid).Append(" from BookTrader.com</description>");
        return title.ToString();
    }

    /// <summary>
    /// generates the rss code for each link
    /// </summary>
    /// <param name="tradeType"></param>
    /// <param name="message"></param>
    /// <param name="link"></param>
    /// <returns></returns>
    private string ItemGenerator(string tradeType, string message, string link) //generates each item
    {
        StringBuilder item = new StringBuilder();
        item.Append("<item>");
        item.Append("<title>").Append(tradeType).Append("</title>");
        item.Append("<description>").Append(message).Append("</description>");
        item.Append("<link>").Append(link).Append("</link>");
        item.Append("</item>");
        return item.ToString();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

internal class Notification
{
    //post data obtained from database
    private string _postPerson;
    private string _postTitle;
    private string _postMessage;
    private string _postLink;
    
    internal string GetPerson
    {
        get { return _postPerson; }
    }
    
    internal string GetTitle
    {
        get{ return _postTitle; }
    }
    
    internal string GetMessage
    {
        get{ return _postMessage; }
    }
    
    internal string GetLink
    {
        get{ return _postLink; }
    }
    
    internal Notification(string person, string title, string message, string link)
    {
        _postPerson = person;
        _postTitle = title;
        _postMessage = message;
        _postLink = link;
    }
}